package com.car.advertising.db;

import android.content.Context;

import com.car.advertising.CarApplication;

import java.util.List;

import de.greenrobot.dao.query.QueryBuilder;

/**
 * 开发者：LuoYi
 * Time: 2017 23:02 2017/5/4 05
 */

public class UserInfoBiz {

    private static UserInfoBiz instance;
    private static Context mContext;
    private UserInfoEntiyDao mDao;

    public static UserInfoBiz getInstance(Context context) {
        if (instance == null) {
            instance = new UserInfoBiz();
            if (mContext == null) {
                mContext = context.getApplicationContext();
            }
        }
        DaoSession daoSession = CarApplication.getDaoSession(context);
        instance.mDao = daoSession.getUserInfoEntiyDao();
        return instance;
    }

    /**
     * 添加一个info
     */
    public void saveMerchantAttribute(final List<UserInfoEntiy> cityInfoList) {
        if (cityInfoList == null || cityInfoList.isEmpty()) {
            return;
        }
        mDao.getSession().runInTx(new Runnable() {

            @Override
            public void run() {
                for (int i = 0; i < cityInfoList.size(); i++) {
                    UserInfoEntiy userInfoEntiy = cityInfoList.get(i);
                    mDao.insertOrReplace(userInfoEntiy);
                }
            }
        });
    }

    public void saveMerchant(final UserInfoEntiy cityInfo) {
        if (cityInfo == null) {
            return;
        }
        mDao.getSession().runInTx(new Runnable() {

            @Override
            public void run() {
                mDao.insertOrReplace(cityInfo);
            }
        });
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<UserInfoEntiy> loadAllData() {
        QueryBuilder<UserInfoEntiy> queryBuilder = mDao.queryBuilder();
        return queryBuilder.list();
    }

    public UserInfoEntiy loadData() {
        QueryBuilder<UserInfoEntiy> queryBuilder = mDao.queryBuilder();
        List<UserInfoEntiy> userInfoEntiys = queryBuilder.list();
        if (userInfoEntiys.size() != 0) {
            return userInfoEntiys.get(0);
        }
        return null;
    }


    /**
     * 删除一个info
     *
     * @param info
     */
    public void delete(UserInfoEntiy info) {
        mDao.delete(info);
    }

    /**
     * 删除全部数据
     */
    public void deleteAllInfo() {
        mDao.deleteAll();
    }

}
